package com.example.demo.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User selectUserByUser(User user) {
        QueryWrapper queryWrapper = new QueryWrapper(user);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<User> selectUserList() {
        return userMapper.selectList(null);
    }

    @Override
    public int insertUserByUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public Integer getUserListCount(User user) {
        return userMapper.getUserListCount(user);
    }

    @Override
    public int updateUserByUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUserById(User user) {
        return userMapper.deleteById(user);
    }

    @Override
    public List<User> selectUserListByPage(User user) {
        return userMapper.selectUserListByPage(user);
    }

    @Override
    public Page<User> selectUserListByMybatisPlusPage(User user, Page<User> page) {
        //selectPage(page类型，查询条件)
        QueryWrapper queryWrapper = new QueryWrapper();
        if (user.getId() != null) {
            queryWrapper.eq("id", user.getId());
        }
        if (user.getUsername() != null && (!user.getUsername().equals(""))) {
            queryWrapper.eq("username", user.getUsername());
        }
        if (user.getGender() != null && (!user.getGender().equals(""))) {
            queryWrapper.eq("gender", user.getGender());
        }
        return userMapper.selectPage(page, queryWrapper);
    }

    @Override
    public User selectUserByParentId(int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public int removeByList(int[] ids) {
        return userMapper.removeByList(ids);
    }

    @Override
    public int removeByListMybatisPlus(Integer[] ids) {
//        Arrays.asList() ：将数组转换为集合的方法
//        注意：
//        （1）只适用于对象型的数据类型（引用数据类型 String Integer ...）
        List<Integer> list = Arrays.asList(ids);
//        QueryWrapper queryWrapper = new QueryWrapper();
//        Object ...values ：表示对象型的数据
//        queryWrapper.in("id",Object ...values)
        //deleteBatchIds（）参数类型： （只能是集合类型）Collection最大的集合的接口
        return userMapper.deleteBatchIds(list);
    }

    @Override
    public int updateUserAvatarByUserId(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", username);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public Integer getAuthorityIdByUserId(Integer userId) {
        return userMapper.getAuthorityIdByUserId(userId);
    }

    @Override
    public String getAuthorityNameByUserId(Integer userId) {
        return userMapper.getAuthorityNameByUserId(userId);
    }

    @Override
    public int updateUserAuthorityByUser(User user) {
        return userMapper.updateUserAuthorityByUser(user);
    }


}
